package com.cddeskywebsite.servlet;

import com.cddeskywebsite.bean.NewsBean;
import com.cddeskywebsite.dao.NewsDao;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 1.接受post网址 /newsAdd。jsp
 * 2.接收到传递过来的新闻参数 新闻标题 新闻内容
 * 3.完成新闻的添加
 * 4.使当前网页刷新(或者跳转到其他页面)
 */
@WebServlet("/newsAdd.jsp")
public class NewsAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsTitle = req.getParameter("NewsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean newsBean = new NewsBean();//实例化新闻对象
        newsBean.setNewsTitle(newsTitle);//设置新闻标题
        newsBean.setNewsContent(newsContent);//设置新闻内容
        NewsDao newsDao = new NewsDao();//实例化新闻Dao对象
        newsDao.add(newsBean);//调用添加方法 传递新闻对象
        // 在Servlet中调用重定向方法，可以将页面跳转到其他页面
        resp.sendRedirect("news.jsp");
    }
}
